﻿
CREATE FUNCTION [dbo].[WorkFlowToByGroup](
	@StatusNameSpace sysname,
	@GroupName sysname,
	@MenuFromID int
)RETURNS TABLE AS

RETURN(
WITH Menus(NextMenuID,MNextenuPath,Direction)AS(
	SELECT wf.NextMenuID,wf.NextMenuPath,Direction
	FROM v_WorkFlow wf INNER JOIN t_Workflow_Groups AS wfg
	ON wf.MenuID = wfg.MenuID AND wf.NextMenuID = wfg.NextMenuID AND GroupName = @GroupName
	WHERE wf.MenuID = @MenuFromID AND wf.StatusNameSpace = @StatusNameSpace
)
SELECT DISTINCT TOP 100 PERCENT wf.NextMenuID,wf.NextMenuPath,CONVERT(bit,ISNULL(Menus.NextMenuID,0)) HasAccess,wf.Direction,wf.NextWFIndex
FROM v_WorkFlow wf LEFT OUTER JOIN Menus ON Menus.NextMenuID = wf.NextMenuID
WHERE wf.MenuID = @MenuFromID AND wf.StatusNameSpace = @StatusNameSpace AND wf.NextMenuID <> 25
ORDER BY wf.NextWFIndex
)

